package miui.xmpp.message;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilderFactory;

/**
 * Created by ouyang on 15-6-1.
 */
public class Message {

    private String id = null;
    private String body = null;

    public static Message create(byte bytes[]) {
        Message thiz = null;

        do {
            if (bytes == null) {
                break;
            }

            thiz = new Message();
            if (! thiz.load(bytes)) {
                thiz = null;
                break;
            }

        } while (false);

        return thiz;
    }

    public Message() {
    }

    public Message(String id, String body) {
        this.id = id;
        this.body = body;
    }

    public Message(int id, String body) {
        String idStr = String.format("%d", id);
        this.id = idStr;
        this.body = body;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setId(int id) {
        String idStr = String.format("%d", id);
        this.id = idStr;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public boolean load(byte bytes[]) {
        boolean result = false;

        do {
            if (bytes == null) {
                break;
            }

            InputStream is = new ByteArrayInputStream(bytes);
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();

            try {
                JSONObject json = new JSONObject(new String(bytes));
                if (json == null) {
                    break;
                }

                json = json.optJSONObject("message");
                if (json == null) {
                    break;
                }

                id = json.optString("id");
                body = json.optString("body");
                result = true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } while (false);

        return result;
    }

    private Element getTag(Element node, String tag) {
        if (node == null) {
            return null;
        }

        NodeList tags = node.getElementsByTagName("*");
        for (int i = 0; i < tags.getLength(); ++i) {
            Element child = (Element) tags.item(i);
            if (child.getTagName().equalsIgnoreCase(tag)) {
                return child;
            }
        }

        return null;
    }

    @Override
    public String toString() {

        do {
            if (id == null) {
                break;
            }

            JSONObject message = new JSONObject();
            JSONObject json = new JSONObject();
            try {
                json.put("id", id);
                json.put("body", body);
                message.put("message", json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return message.toString();
        } while (false);

        return "";
    }
}
